import {
  request
} from '../../utils/requestUtils';

import {
  userBehavior
} from '../../behaviors/userBehavior'

Page({
  behaviors: [userBehavior],
  data: {
    form: {
      lines: [], //行信息
    }
  },
  onLoad(options) {
    if (options.id) {
      this.getById(options.id)
    }
  },
  //获取详情
  async getById(id) {
    const result = await request({
      url: `/bus/merchant/banner/${id}`,
      method: "GET",
    })
    if (result.data.code == 200) {
      // 下单时间超过12小时不让退货
      this.setData({
        form: result.data.data
      })
      this.selectComponent('#cover').initListHandler([{
        url: result.data.data.cover
      }])
      this.selectComponent('#video').initListHandler([{
        url: result.data.data.videoUrl
      }])
    }
  },
  // 选择上下架状态
  sekectStatus() {
    let component = this.selectComponent('#dictCon')
    console.log('component', component)
    if (component) {
      component.addhow('product_status');
    }
  },
  // 数据字典返回
  dictConfirm(e) {
    if (e.detail.key == "product_status") {
      this.setData({
        'form.status': e.detail.dictValue,
        'form.statusName': e.detail.dictLabel,
      })
    }
  },
  onChange(event) {
    switch (event.currentTarget.dataset['input']) {
      case 'title':
        this.setData({
          "form.title": event.detail,
        })
        break;
      case 'remark':
        this.setData({
          "form.remark": event.detail,
        })
        break;
    }
  },
  // 保存数据
  async submitOrder() {
    let cover = this.selectComponent('#cover').data.fileList
    let video = this.selectComponent('#video').data.fileList
    if (!cover || cover.length == 0) {
      wx.showToast({
        title: '封面不能为空',
        icon: 'none'
      })
      return
    }
    if (!video || video.length == 0) {
      wx.showToast({
        title: '视频不能为空',
        icon: 'none'
      })
      return
    }
    const result = await request({
      url: `/bus/merchant/banner`,
      method: "PUT",
      data: Object.assign(this.data.form, {
        cover: cover[0].url,
        url: video[0].url,
        type: 2
      })
    })
    if (result.data.code == 200) {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  // 新增类型
  addProd() {
    console.log('选择商品')
    let component = this.selectComponent('#addItem')
    if (component) {
      component.addShow();
    }
  },
  // 选择商品
  choose(e) {
    if (!e.detail || e.detail.length == 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      })
      return
    }
    e.detail.forEach(v => {
      if (!this.data.form.lines) this.data.form.lines = []
      console.log('ss', this.data.form.lines)
      let e = this.data.form.lines.find(e => e.productId == v.id)
      if (e) return
      this.data.form.lines.push({
        productId: v.id,
        productName: v.productName,
        productDesc: v.productDesc,
        productUrl: v.listPic,
        price: v.prePrice,
        attr1: v.unitName,
      })
    })
    this.setData({
      'form.lines': this.data.form.lines
    })
  },
  async deleteItem(e) {
    const modalRes = await wx.showModal({
      content: '确定删该商品?'
    })
    if (modalRes.cancel) return
    this.setData({
      'form.lines': this.data.form.lines.filter(item => item.productId !== e.target.dataset.itemid)
    })
  }
})